//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package servlet.register;

import dao.Func;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONObject;
import utils.JDBCUtils;

@WebServlet({"/DoUpdateRegister"})
public class DoUpdateRegister extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public DoUpdateRegister() {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/json; charset=utf-8");
        PrintWriter out = response.getWriter();
        String id = "";
        String in_time = "";
        id = request.getParameter("id");
        in_time = URLDecoder.decode(request.getParameter("in_time"), "UTF-8");
        HttpSession session = request.getSession();
        if (session.getAttribute("username") != null && !session.getAttribute("username").toString().equals("")) {
            Connection connection = null;
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;

            try {
                connection = JDBCUtils.getConnection();
                JSONObject jsonmain = new JSONObject();
                String sql = "update in_or_out set out_time=?,count_time=? where id=?";
                preparedStatement = connection.prepareStatement(sql);
                String outTime = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
                preparedStatement.setString(1, outTime);
                preparedStatement.setString(2, Func.getTimeDifference(outTime, in_time));
                preparedStatement.setString(3, id);
                int result = preparedStatement.executeUpdate();
                if (result != 0) {
                    response.setStatus(200);
                    jsonmain.put("code", "200");
                    jsonmain.put("msg", "updated");
                } else {
                    response.setStatus(500);
                    jsonmain.put("code", "500");
                    jsonmain.put("msg", "Error");
                }

                out.println(jsonmain);
            } catch (ClassNotFoundException var18) {
                ClassNotFoundException e = var18;
                e.printStackTrace();
            } catch (SQLException var19) {
                SQLException e = var19;
                e.printStackTrace();
            } finally {
                JDBCUtils.release((ResultSet)resultSet, preparedStatement, connection);
            }

        } else {
            response.setStatus(403);
            out.println("403 Forbidden");
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }
}
